<?php 
HeaderScripts::addScripts(array(
	'admin/spinner.js',
	'admin/spinnerField.js',
	'admin/multiSelect.js',
	'admin/itemSelector.js',
	'admin/fileUploadField.js'
));
HeaderScripts::addStyles(array(
	'admin/spinner.css',
	'admin/multiSelect.css',
	'admin/fileUploadField.css',
	'admin/form.css'
));
?>
<div style="display:none;">
<textarea name="addressTxt" id="addressTxt">
<?php echo htmlspecialchars($hotel->attributes['address']) ?>
</textarea>
<textarea name="transferTxt" id="transferTxt">
<?php echo htmlspecialchars($hotel->attributes['transfer']) ?>
</textarea>
<textarea name="descriptionTxt" id="descriptionTxt">
<?php echo htmlspecialchars($hotel->attributes['description']) ?>
</textarea>
<textarea name="roomsTxt" id="roomsTxt">
<?php echo htmlspecialchars($hotel->attributes['rooms']) ?>
</textarea>
</div>
<script type="text/javascript">
var citiesArr  = <?php echo CJSON::encode($cities)?>;
var selectedExcursions    = <?php echo CJSON::encode($selectedExcursions)?>;
var notSelectedExcursions = <?php echo CJSON::encode($notSelectedExcursions)?>;
var selectedHotelFeatures    = <?php echo CJSON::encode($selectedHotelFeatures)?>;
var notSelectedHotelFeatures = <?php echo CJSON::encode($notSelectedHotelFeatures)?>;
var selectedRoomFeatures    = <?php echo CJSON::encode($selectedRoomFeatures)?>;
var notSelectedRoomFeatures = <?php echo CJSON::encode($notSelectedRoomFeatures)?>;

Ext.namespace('Ext.cities');

Ext.onReady(function(){
	
	Ext.cities.data = citiesArr;
	
	var ds = new Ext.data.ArrayStore({
	        data: notSelectedExcursions,
	        fields: ['value','text'],
	        sortInfo: {
	            field: 'text',
	            direction: 'ASC'
	        }
	});
	
	var ds2 = new Ext.data.ArrayStore({
	        data: notSelectedHotelFeatures,
	        fields: ['value','text'],
	        sortInfo: {
	            field: 'text',
	            direction: 'ASC'
	        }
	});
	
	var ds3 = new Ext.data.ArrayStore({
	        data: notSelectedRoomFeatures,
	        fields: ['value','text'],
	        sortInfo: {
	            field: 'text',
	            direction: 'ASC'
	        }
	});

    Ext.QuickTips.init();

    // turn on validation errors beside the field globally
    Ext.form.Field.prototype.msgTarget = 'side';

    var formhotelcreate = new Ext.FormPanel({
        labelWidth: 75, // label settings here cascade unless overridden
        standardSubmit: true,
		fileUpload: true,
		url: '/admin/hotel/createhandler/',
        frame:true,
        title: 'Редактировать отель',
        bodyStyle:'padding:5px 5px 0',
        width: 600,

        items: [{
            xtype:'fieldset',
            title: 'Общая информация',
            collapsible: true,
            autoHeight:true,
            defaults: {width: 210},
			labelWidth: 130,
            defaultType: 'textfield',
            items :[{
                    fieldLabel: 'Название',
                    name: 'Hotel[title]',
                    value: '<?php echo addcslashes($hotel->attributes['title'], "'\\") ?>',
                    allowBlank:false,
					blankText: 'Поле обязательно для заполнения'
			}, new Ext.form.ComboBox({
                    fieldLabel: 'Город',
                    hiddenName: 'Hotel[cityId]',
                    store: new Ext.data.ArrayStore({
                        fields: ['value', 'city'],
                        data : Ext.cities.data
                    }),
                    displayField: 'city',
					valueField: 'value',
					value: '<?php echo intval($hotel->attributes['cityId']) ?>',
                    typeAhead: true,
                    mode: 'local',
                    editable: false,
                    triggerAction: 'all',
                    emptyText: 'Выберите город',
                    selectOnFocus:true,
                    width: 200,
					allowBlank:false,
					blankText: 'Поле обязательно для заполнения'
			}),{
					fieldLabel: 'Адрес',
            		name: 'Hotel[address]',
					xtype:'textarea',
					height: 60,
					value: document.getElementById('addressTxt').value
			},{
					xtype: 'spinnerfield',
					fieldLabel: 'Количество звезд',
					name: 'Hotel[stars]',
					minText: 'Минимальное допустимое значение: {0}',
					minValue: 0,
					maxValue: 5,
					allowDecimals: false,
					decimalPrecision: 1,
					incrementValue: 1,
					accelerate: true,
					value: <?php echo intval($hotel->attributes['stars']) ?>
            },{
                fieldLabel: 'Валюта',
                name: 'Hotel[currency]',
                value: "<?php echo htmlspecialchars($hotel->attributes['currency'], ENT_QUOTES) ?>"
			}]
        },{
            xtype:'fieldset',
            title: 'Экскурсии',
            collapsible: true,
            autoHeight:true,
			labelWidth: 130,
            defaults: {width: 420},
            defaultType: 'textfield',
            items :[{
					name: 'Hotel[availableTours]',
					id:'availableTours',
					fieldLabel: 'Наличие экскурсий',
					xtype:'checkbox',
					checked: <?php if (intval($hotel->attributes['availableTours']) == 1) { echo 'true'; } else { echo 'false'; } ?>,
					inputValue: 1
			},{
            		xtype: 'itemselector',
            		name: 'Hotel2excursion',
            		fieldLabel: 'Экскурсии',
	        		imagePath: '/public/assets/extjs/resources/images/default/multiselect/',
	        		height: 200,
            		multiselects: [{
						dragGroup: 'toursDD',
						dropGroup: 'toursDD',
						width: 200,
                		height: 200,
                		cls: 'multiselectStr',
                		store: ds,
                		displayField: 'text',
                		valueField: 'value'
            		},{
						dragGroup: 'toursDD',
						dropGroup: 'toursDD',
						width: 200,
                		height: 200,
                		cls: 'multiselectStr',
                		store: selectedExcursions,
						tbar:[{
                    		text: 'очистить',
                    		handler:function(){
	                    		formhotelcreate.getForm().findField('Hotel2excursion').reset();
	                		}
                		}]
            		}]
        	}]
        },{
            xtype:'fieldset',
            title: 'Описание отеля',
            collapsible: true,
            autoHeight:true,
			labelWidth: 130,
            defaults: {width: 420},
            defaultType: 'textfield',
            items :[{
					name: 'Hotel[description]',
					id:'description',
					fieldLabel: 'Описание',
					xtype:'textarea',
					height: 100,
					value: document.getElementById('descriptionTxt').value
			},{
                    fieldLabel: 'Номера',
                    name: 'Hotel[rooms]',
					xtype:'textarea',
					width: 420,
					height: 100,
					value: document.getElementById('roomsTxt').value
			},{
                	fieldLabel: 'Трансфер',
                	name: 'Hotel[transfer]',
					xtype:'textarea',
					width: 420,
					height: 100,
					value: document.getElementById('transferTxt').value
			},{
            		xtype: 'itemselector',
            		name: 'Hotel2hotelFeatures',
            		fieldLabel: 'Фишки отеля',
	        		imagePath: '/public/assets/extjs/resources/images/default/multiselect/',
					valueField: 'value',
					height: 200,
            		multiselects: [{
            			width: 200,
                		height: 200,
                		cls: 'multiselectStr',
                		store: ds2,
                		displayField: 'text',
                		valueField: 'value',
						dragGroup: 'hotelDD',
						dropGroup: 'hotelDD'
            		},{
						dragGroup: 'hotelDD',
						dropGroup: 'hotelDD',
						valueField: 'value',
						width: 200,
                		height: 200,
                		cls: 'multiselectStr',
                		store: selectedHotelFeatures,
						tbar:[{
                    		text: 'очистить',
                    		handler:function() {
	                    		formhotelcreate.getForm().findField('Hotel2hotelFeatures').reset();
	                		}
                		}]
            		}]
        	},{
            		xtype: 'itemselector',
            		name: 'Hotel2roomFeatures',
            		fieldLabel: 'Фишки номера',
	        		imagePath: '/public/assets/extjs/resources/images/default/multiselect/',
					valueField: 'value',
					height: 200,
            		multiselects: [{
                		dragGroup: 'roomDD',
						dropGroup: 'roomDD',
						width: 200,
                		height: 200,
                		cls: 'multiselectStr',
                		store: ds3,
                		displayField: 'text',
                		valueField: 'value'
            		},{
                		dragGroup: 'roomDD',
						dropGroup: 'roomDD',
						width: 200,
                		height: 200,
                		cls: 'multiselectStr',
						valueField: 'value',
                		store: selectedRoomFeatures,
						tbar:[{
                    		text: 'очистить',
                    		handler:function(){
	                    		formhotelcreate.getForm().findField('Hotel2roomFeatures').reset();
	                		}
                		}]
            		}]
        	}]
        }<?php if ($imagesConfig['maxPhotos'] > 0) { ?>,{
			xtype: 'fieldset',
			title: 'Фотографии',
			collapsible: true,
			autoHeight: true,
			labelWidth: 130,
			defaults: {
				width: 450
			},
			items: [<?php
					$hotelPhotos = $hotel->getRelated('hotelPhotos');
					$counter = 1;
					foreach ($hotelPhotos as $photo) {
						if ($counter <= $imagesConfig['maxPhotos']) { ?>{
							xtype: 'fieldset',
							layout:'column',
							width: 450,
							style: 'border: 0px; padding: 0px;',
							items: [{
								xtype: 'label',
								text: 'Фото #<?php echo $counter ?>:',
								style: 'font-size: 12px',
								width: 135
							},{
								xtype: 'fieldset',
								style: 'border: 0px;',
								width: 450,
								items: [{
									xtype: 'displayfield',
									anchor: '100%',
									value: '<a href="javascript: openBigImage(<?php echo $photo->id ?>,\'<?php echo $photo->image ?>\')"><img src="<?php echo $imagesConfig['uploadPath'] ?><?php echo $imagesConfig['mediumImgPrefix'] ?>_<?php echo $photo->image ?>"></a>'
								},{
									name: 'HotelPhotoRemove[]',
									xtype:'checkbox',
									fieldLabel: 'Удалить',
									labelStyle: 'padding-left: 130px; width: 55px',
									checked: false,
									inputValue: <?php echo intval($photo->id) ?>
								},{
									xtype: 'fileuploadfield',
									id: 'photoField<?php echo $counter ?>',
									emptyText: 'Выберите изображение',
					            	name: 'HotelPhoto[reload<?php echo intval($photo->id) ?>]',
					            	buttonText: '',
									width: 250,
					            	buttonCfg: {
					                	iconCls: 'upload-icon'
					            	}
								}, {
									name: 'HotelPhotoReload[<?php echo intval($photo->id) ?>]',
									xtype:'hidden',
									value: 1
								}]
							}]
				        }<?php
				        		if ($counter != $imagesConfig['maxPhotos']) {
				        			echo ',';
				        		}
								$counter++;
							}
						}
					for ($i = $counter; $i <= $imagesConfig['maxPhotos']; $i++) {?>{
						xtype: 'fileuploadfield',
		            	id: 'photoField<?php echo $i ?>',
		            	emptyText: 'Выберите изображение',
		            	fieldLabel: 'Фото #<?php echo $i ?>',
		            	name: 'HotelPhoto[]',
		            	buttonText: '',
						width: 250,
		            	buttonCfg: {
		                	iconCls: 'upload-icon'
		            	}
					}<?php
						if ($i != $imagesConfig['maxPhotos']) {
							echo ',';
						}
					} ?>]
		}<?php } ?>],

        buttons: [{
            text: 'Сохранить',
			handler: function(b) {
				var fp = this.ownerCt.ownerCt,
					form = fp.getForm();
				if (form.isValid()) {
					b.disable();
					form.getEl().dom.action = '/admin/hotel/edithandler/id/<?php echo $hotel->attributes['id'] ?>/';
					form.submit();
            	} else {
					formhotelcreate.form.markInvalid();
					Ext.MessageBox.alert('Ошибка!', 'Ошибка, информация неверная! Проверьте правильность заполнения полей!');
				}
			}
        },{
            text: 'Отменить',
            handler: function() {
            	location.href = '/admin/hotel/';
            }
        }]
    });

    formhotelcreate.render(Ext.get('hoteleditdiv'));
});

function openBigImage(id, image)
{
	window.open("<?php echo $imagesConfig['uploadPath'] ?><?php echo $imagesConfig['bigImgPrefix'] ?>_" + image, "imgWin" + id, "width=<?php echo $imagesConfig['maxWidth'] ?>,height=<?php echo $imagesConfig['maxHeight'] ?>,left=300,top=300,status=no,toolbar=no,menubar=no");
}
</script>
<div id="hoteleditdiv"></div>